module.exports = function ($resource, $state, $http, $q, $modal, dlq, $timeout) {
	return {
		restrict: 'AE',
		template: require('../views/p_docking.html'),
		replace: true,
		scope: {
			'p' : '=',
			//'other': '=',
			//'state' : '=',
			//'vm' : '=',
		},
		link: function (scope, elements, attrs) {

			console.log('docking');

			console.log(scope.p);

scope.save = function(item){

	console.log(item);

	var para = {
		'id' : item.attr_id,
		'sale_code' : item.sale_code, 
	};

    //票付通
    if(scope.p.sale_belong === 'weshop_piaofutong')
    {
        para['sysaffirm_target_goods_supply'] = item.sysaffirm_target_goods_supply;
        para['sysaffirm_target_goods_view'] = item.sysaffirm_target_goods_view;
        para['sysaffirm_target_goods_code'] = item.sysaffirm_target_goods_code;
    }
    //居游
    else if(scope.p.sale_belong === 'weshop_juyou')
    {
        para['sysaffirm_target_goods_code'] = item.sysaffirm_target_goods_code;
    }
    //天时同城（北国）/天时同城（爱旅）
    else if(scope.p.sale_belong === 'weshop_tstc_al'
         || scope.p.sale_belong === 'weshop_tstc_bg')
    {
        para['sysaffirm_target_goods_code'] = item.sysaffirm_target_goods_code;
    }
    //海洋馆
    else if(scope.p.sale_belong === 'weshop_royaloc')
    {
        para['sysaffirm_target_goods_code'] = item.sysaffirm_target_goods_code;
        para['sysaffirm_target_goods_back_type'] = item.sysaffirm_target_goods_back_type;
        para['sysaffirm_target_goods_child_flag'] = item.sysaffirm_target_goods_child_flag;
    }


    if(scope.p.sale_belong === 'weshop_juyou'){
    	$resource('/api/as/tc/sale/wdinfo', {}, {}).save({'code' : item.sysaffirm_target_goods_code}, function (res) {
			console.log(res);
			if(res.errcode != 0){
				alert(res.errmsg);
				return;
			}
			if(res.data.sale_belong != 'juyou') {
				alert('只能是居游产品，不能是对接其他系统的产品');
				return;
			}
			$resource('/api/ac/wc/newProductService/createAttr', {}, {}).save(para, function(res){
				console.log(res);
				if (res.errcode !== 0) {
					alert(res.errmsg);
					return;
				}
				alert('保存成功')
			});
		});
    }else{
    	$resource('/api/ac/wc/newProductService/createAttr', {}, {}).save(para, function(res){
			console.log(res);
			if (res.errcode !== 0) {
				alert(res.errmsg);
				return;
			}
			alert('保存成功')
		});
    }
    





	// $resource('/api/ac/wc/newProductService/createAttr', {}, {}).save(para, function(res){
	// 	console.log(res);
	// 	if (res.errcode !== 0) {
	// 		alert(res.errmsg);
	// 		return;
	// 	}
	// 	alert('保存成功')
	// });
}


scope.cancel = function () {
    $modalInstance.close();
}
			
		}
	};
};

